import pygame
import auxs
from defs import *

class Draw():
    def __init__(self, screen):
        self.screen = screen
        self.back = False

        # Make pause menu
        self.dimmedSurface = pygame.Surface( \
                pygame.display.get_surface().get_size())
        self.dimmedSurface.fill(black)
        self.dimmedSurface.set_alpha(alphaDimFactor)

        # Lists of objects
        self.numbers    = []
        self.timers     = []
        self.ellipses   = []
        self.rectangles = []
        self.sprites    = []
        self.level      = []
        self.numPosition    = []
        self.timePosition   = []

    def __init__(self, screen, backgroundImage=None):
        self.screen = screen               
        self.backgroundImage = backgroundImage
        if not backgroundImage == None:
			self.back = True
        else:
			self.back = False

        # Make pause menu
        #~ self.dimmedSurface = pygame.Surface( \
                #~ pygame.display.get_surface().get_size())
        #~ self.dimmedSurface.fill(black)
        #~ self.dimmedSurface.set_alpha(alphaDimFactor)

        # List of objects
        self.numbers    = []
        self.timers     = []        
        self.ellipses   = []
        self.rectangles = []
        self.sprites    = []
        self.level      = []
        self.numPosition    = []
        self.timePosition   = []

    def setColors(self, back_color, fill_color):
        self.color_back = back_color
        self.color_fill = fill_color

    def setBackColor(self, back_color):
        self.color_back = back_color

    def setFillColor(self, fill_color):
        self.color_fill = fill_color

    def setBack(self, backgroundImage):        
        self.backgroundImage = backgroundImage

    def setRect(self, objects):
        self.rectangles = objects

    def setEllipse(self, objects):
        self.ellipses = objects

    def setSprite(self, objects):
        self.sprites = objects
    
    def setNumber(self, objects):
        self.numbers = objects
    
    def setTime(self, objects):
        self.timers = objects
    
    def setNumPosition(self, objects):
        self.numPosition = objects

    def setTimePosition(self, objects):
        self.timePosition = objects

    def updateSpritePosition(self, positions):
        #~ print len(self.sprites)
        for i in range(len(self.sprites)):
            self.sprites[i][1] = positions[i]

    # Takes a list of sprites, and return a list of 
    # (sprite, position)

    # Takes two lists l1, l2 of the same length and 
    # return a list of pairs
    # res = [[l1[0], l2[0]], ..., [l1[n-1], l2[n-1]]]
    def makePairList(self, first, second):
        res = []
        for i in range(len(first)):
            pair = [first[i], second[i]]
            res.append(pair)
        return res

    # Takes a list of lists and returns a list
    def makeList(self, lists):
        res = []
        for l in lists:
            for e in l:
                res.append(e)
        return res
        #~ res = [item for sublist in l for item in sublist]

    def updateBack(self):
        if self.back:
            self.screen.blit(self.backgroundImage, (0,0))
        #~ else:
            #~ self.screen.fill(self.color_back)

    def drawSprite(self, objects):
        for o in objects:
            self.screen.blit(o[0], o[1])			

    def drawRect(self, objects):		
        for o in objects:
            pygame.draw.rect(self.screen, self.color_fill, o)

    def drawEllipse(self, objects):		
        for o in objects:
            pygame.draw.ellipse(self.screen, self.color_fill, o)
            
    def dim(self):		
        dimmedSurface = pygame.Surface(pygame.display.get_surface().get_size())
        dimmedSurface.fill(black)
        dimmedSurface.set_alpha(alphaDimFactor)
        self.screen.blit(dimmedSurface,(0,0))
        pygame.display.flip()

    # Updates the rendering of the numbers using the font
    def renderNumber(self, font, objects):
        self.numbers = []
        for o in objects:
            self.numbers.append(font.render(str(o), 1, white))        

    def renderTime(self, font, objects):
        self.timers = []
        for o in objects:
            self.timers.append(font.render(o[1] + \
                auxs.getTimerString(int(o[0])), 1, white))        

    def updateLevel(self, level, font):
        self.level = font.render("Level " + str(level), 1, white)
        
    def updatePause(self, font):
        draw_pause  = font.render(pause_text, 1, white)
        self.drawSprite([[draw_pause, pause_text_pos]])
        pygame.display.flip()
                
    def updateMenu(self, font):
        draw_menu_start = font.render(menu_start, 1, white)     
        draw_menu_p1    = font.render(menu_p1, 1, white)   
        draw_menu_p2    = font.render(menu_p2, 1, white)   
        self.drawSprite([[draw_menu_start, menu_start_pos], \
                            [draw_menu_p1, menu_p1_pos], \
                            [draw_menu_p2, menu_p2_pos]])
        pygame.display.flip()

    def displayMainMenu(self):
		font = pygame.font.Font(None, 36)
		draw_menu_main_start = font.render(main_menu_start, 1, white)     
		draw_menu_main_ach = font.render(main_menu_ach, 1, white)     
		draw_menu_main_exit = font.render(main_menu_exit, 1, white)     
        
		self.drawSprite([[draw_menu_main_start, main_menu_start_pos], \
                            [draw_menu_main_ach, main_menu_ach_pos], \
                            [draw_menu_main_exit, main_menu_exit_pos]])
		pygame.display.flip()
		
    def update(self):
        #~ num = self.renderNumber(self.numbers)
        #~ time = self.renderTime(self.timers)
        spriteNum = self.makePairList(self.numbers, self.numPosition)
        spriteTime = self.makePairList(self.timers, self.timePosition)
        
        #~ No mostrar el nivel si el juego no empezo
        if self.level:
            spriteLevel = [[self.level, pos_lvl]]
            allSprites = self.makeList([spriteNum, spriteTime, spriteLevel, self.sprites])
        else:
            allSprites = self.makeList([spriteNum, spriteTime, self.sprites])
            
        self.updateBack()
        self.drawSprite(allSprites)
        self.drawEllipse(self.ellipses)
        self.drawRect(self.rectangles)        
        pygame.display.flip()     
